package com.os;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import mapgenerator.Main;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

public class OSDialogCreateSprite extends JDialog implements ActionListener {

    private JButton Cancel;
    private JButton OK;
    private ButtonGroup buttonGroup1;
    private JLabel jLabelCreateSprite;
    private JLabel jLabelName;
    private JLabel jLabelType;
    private JRadioButton jRadioButtonCharacter;
    private JRadioButton jRadioButtonOther;
    private JTextField name;
    private Main comp;

    public OSDialogCreateSprite(Main comp) {
        this.comp = comp;
        initComponents();
    }

    private void initComponents() {

        buttonGroup1 = new ButtonGroup();
        jLabelCreateSprite = new JLabel();
        jLabelName = new JLabel();
        jLabelType = new JLabel();
        jRadioButtonCharacter = new JRadioButton();
        jRadioButtonOther = new JRadioButton();
        name = new JTextField();
        OK = new JButton();
        Cancel = new JButton();

        setTitle("Create New Sprite");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(350, 260));
        setLocationByPlatform(true);
        setResizable(false);
        setLayout(new AbsoluteLayout());

        jLabelCreateSprite.setText("Create Sprite...");
        add(jLabelCreateSprite, new AbsoluteConstraints(10, 10, -1, -1));

        jLabelName.setText("Name:");
        add(jLabelName, new AbsoluteConstraints(20, 60, -1, -1));

        jLabelType.setText("Type:");
        add(jLabelType, new AbsoluteConstraints(20, 100, -1, -1));

        buttonGroup1.add(jRadioButtonCharacter);
        jRadioButtonCharacter.setText("Character");
        jRadioButtonCharacter.setSelected(true);
        add(jRadioButtonCharacter, new AbsoluteConstraints(70, 100, -1, -1));

        buttonGroup1.add(jRadioButtonOther);
        jRadioButtonOther.setText("Other");
        add(jRadioButtonOther, new AbsoluteConstraints(70, 120, -1, -1));
        add(name, new AbsoluteConstraints(70, 60, 225, -1));

        OK.setText("OK");
        OK.addActionListener(this);
        add(OK, new AbsoluteConstraints(140, 180, 75, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(this);
        add(Cancel, new AbsoluteConstraints(220, 180, 75, -1));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == OK) {
            String spriteName = name.getText().trim();
            if (!spriteName.equals("")) {
                ObjectSprite os = new ObjectSprite(spriteName, jRadioButtonCharacter.isSelected());
                comp.osContainer.addSprites(os);
                comp.osPanelWidget.reloadAll();
            }else{
                return;
            }
        }
        this.dispose();

    }
}
